﻿#include "Robot.h"
#include "UDeviceConfig.h"
#include "CSqlOper.h"
#include "CRobFactHy.h"
Robot::Robot()
{
	m_nType = 0;
	m_pMod = new CModBusOper("127.0.0.1", 502);
	QVector<int>vecVal, vecVa2, vecVa3;
	for (int i = 0; i < 323; i++)
	{
		vecVa2.push_back(0);
		vecVa3.push_back(i+1);
	}
	int reVal, reVa2, reVa3, reVa4;
	m_pMod->ReadValEx(40001, 323, vecVal);
	m_pMod->WriteValEx(40001, 323, vecVa2);
	//QThread::msleep(3000);
	m_pMod->WriteValEx(40002, 323, vecVa3);
	//for (int i = 0; i < 10000000000; i++)
	//{
	//	m_pMod->ReadValEx(40001, 323, vecVal);
	//	vecVal.clear();
	//	QThread::msleep(50);
	//	//m_pMod->ReadVal(40001, reVal);
	//	//m_pMod->ReadVal(40002, reVa2);
	//	//m_pMod->ReadVal(40003, reVa3);
	//	//m_pMod->ReadVal(40004, reVa4);
	//}

	//m_pMod->ReadValEx(40002, 300, vecVal);
	//m_pMod->ReadValEx(40003, 300, vecVal);
	//m_pMod->WriteValEx(40001, 80, vecVa2);
	//for (int i = 0; i < 10000000000; i++)
	//{
	//	m_pMod->ReadValEx(40001, 80, vecVal);
	//	QThread::msleep(10);
	//	m_pMod->ReadVal(40001, reVal);
	//	m_pMod->ReadVal(40002, reVa2);
	//	m_pMod->ReadVal(40003, reVa3);
	//	m_pMod->ReadVal(40004, reVa4);
	//}
	
}

Robot::Robot(QString sName, QString sId, QString sConfig)
{
	m_sName = sName;
	m_sId = sId;
	m_sConfig = sConfig;
	m_pConfoig = nullptr;
	m_nType = 0;
	init();
	CPublicData::instance().m_pMapIFDev.insert(m_pDevData->m_sDevGuid, (Robot*)this);
}

Robot::Robot(QString sName, QString sId, CDevDataBase *pDevData)
{
	m_sName = sName;
	m_sId = sId;
	m_pConfoig = nullptr;
	m_nType = 0;
	m_pDevData = pDevData;
	CPublicData::instance().m_pMapIFDev.insert(m_pDevData->m_sDevGuid, (Robot*)this);
}



Robot::~Robot()
{
	if (m_pPointMgrBase != nullptr)
	{
		delete m_pPointMgrBase;
		m_pPointMgrBase = nullptr;
	}

	if (m_pWorkMngrBase != nullptr)
	{
		delete m_pWorkMngrBase;
		m_pWorkMngrBase = nullptr;
	}

	//if (m_pRobDev != nullptr)
	//{
	//	delete m_pRobDev;
	//	m_pRobDev = nullptr;
	//}
	/*if (m_pBaseData != nullptr)
	{
		delete m_pBaseData;
		m_pBaseData = nullptr;
	}*/

	if (m_pRobMainUi != nullptr)
	{
		delete m_pRobMainUi;
		m_pRobMainUi = nullptr;
	}

	/*if (m_pDevData != nullptr)
	{
		delete m_pDevData;
		m_pDevData = nullptr;
	}*/
}

bool Robot::startDevice()
{
	/**
	* @ Author: Gaojia
	* @ Description: 启动测点数据刷新
	*/
	m_pPointMgrBase->start();
	/**
	* @ Author: Gaojia
	* @ Description: 设备开始工作
	*/
	//......................
	return true;
}

bool Robot::stopDevice()
{
	/**
	* @ Author: Gaojia
	* @ Description: 停止测点数据刷新
	*/
	m_pPointMgrBase->stop();
	/**
	* @ Author: Gaojia
	* @ Description: 设备停止工作
	*/
    //......................
	return false;
}

QWidget * Robot::getDeviceConfigUi()
{
	if (m_pConfoig == nullptr)
	{
		m_pConfoig = new UDeviceConfig(m_pRobDev);
	}
	return m_pConfoig;
}

QWidget * Robot::getMainUi()
{
	return m_pRobMainUi;
	//return m_pRobFactHy->createMainWindow();
}

QWidget * Robot::getPanel(int nId)
{
	return m_pRobFactHy->getPanel(nId);
}

QVector<QWidget*> Robot::getPanelList()
{
	return m_pRobFactHy->getPanelList();
}

QList<CMeasurePoint*> Robot::getMeasurePoints()
{
	return m_pPointMgrBase->getPoint();
}





void Robot::init()
{
	/**
	* @ Author: Gaojia
	* @ Description: 获取设备GUID
	*/
	QString sErr = "";
	m_sGuid = selDeviceInfo(m_sId,m_nType, sErr);
	/**
	* @ Author: Gaojia
	* @ Description: 创建设备
	*/
	switch (m_nType)
	{
	case 0:
		m_pRobFactHy = new CRobFactHy(m_sGuid);
		m_pRobFactHy->createAll(this);
		break;
	default:
		break;
	}	
}




void Robot::addDev()
{
	/**
	* @ Author: Gaojia
	* @ Description: 获取设备GUID
	*/
	QString sErr = "";
	m_sGuid = m_pDevData->m_sDevGuid;
	/**
	* @ Author: Gaojia
	* @ Description: 创建设备
	*/
	switch (m_nType)
	{
	case 0:
		m_pRobFactHy = new CRobFactHy(m_sGuid);
		m_pRobFactHy->addAll(m_pDevData, this);
		break;
	default:
		break;
	}
}


//删除设备
void Robot::deleteDevice()
{
	deleteDeviceInfo(m_sId);
}